﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
using System.Data.SQLite;
using MedicLibrary.Controler;

namespace MedicLibrary.Model
{


    class Thuoc
    {
       
        public int ID { get; set; }
        public String MaThuoc { get; set; }
        public String TenThuoc { get; set; }
        public int NhomThuocID { get; set; }
        public String TenNhom { get; set; }
        public int HangSXID { get; set; }
        public String TenHangSX { get; set; }
        public String QuocGia { get; set; }
        public String HinhThuc { get; set; }
        public String HamLuong { get; set; }
        public String LieuLuongCachDung { get; set; }
        public String ChiDinh { get; set; }
        public String ChongChiDinh { get; set; }
        public String DuocDongHoc { get; set; }
        public String DuocLucHoc { get; set; }
        public String ThanTrong { get; set; }
        public String TacDungPhu { get; set; }
        public String LuuY { get; set; }
        public String GhiChu { get; set; }

        public Thuoc()
        {

        }

        public static DataTable getDanhSachThuoc(String para)
        {
            DBConnector dbc = new DBConnector();
            String sql = "";
            if(para == "xemtatcathuoc")
            {
                sql = "SELECT Thuoc.MaThuoc, Thuoc.TenThuoc, NhomThuoc.TenNhom, HangSX.TenHangSX, Thuoc.HinhThuc, HangSX.QuocGia, Thuoc.GhiChu, Thuoc.ID FROM Thuoc LEFT JOIN NhomThuoc ON Thuoc.NhomThuocID = NhomThuoc.ID";
                sql += " LEFT JOIN HangSX ON Thuoc.HangSXID = HangSX.ID ORDER BY Thuoc.MaThuoc DESC";
            }
            else if (para != "")
            {
                sql = "SELECT Thuoc.MaThuoc, Thuoc.TenThuoc, NhomThuoc.TenNhom, HangSX.TenHangSX, Thuoc.HinhThuc, HangSX.QuocGia, Thuoc.GhiChu, Thuoc.ID FROM Thuoc LEFT JOIN NhomThuoc ON Thuoc.NhomThuocID = NhomThuoc.ID";
                sql += " LEFT JOIN HangSX ON Thuoc.HangSXID = HangSX.ID WHERE Thuoc.TenThuoc LIKE '%" + para + "%' ORDER BY Thuoc.MaThuoc DESC LIMIT 50";
            }
            else
            {
                sql = "SELECT Thuoc.MaThuoc, Thuoc.TenThuoc, NhomThuoc.TenNhom, HangSX.TenHangSX, Thuoc.HinhThuc, HangSX.QuocGia, Thuoc.GhiChu, Thuoc.ID FROM Thuoc LEFT JOIN NhomThuoc ON Thuoc.NhomThuocID = NhomThuoc.ID";
                sql += " LEFT JOIN HangSX ON Thuoc.HangSXID = HangSX.ID ORDER BY Thuoc.MaThuoc DESC LIMIT 50";
            }
            return dbc.GetDataTable(sql);
        }

        public static DataTable getThongTinThuoc(int ID)
        {
            DBConnector dbc = new DBConnector();
            String sql = "SELECT * FROM Thuoc, NhomThuoc, HangSX";
            sql += " WHERE Thuoc.NhomThuocID = NhomThuoc.ID";
            sql += " AND Thuoc.HangSXID = HangSX.ID";
            sql += " AND Thuoc.ID = " + ID;
            return dbc.GetDataTable(sql);

        }

        public bool readThongTinThuoc(int ID)
        {
            DataTable table = getThongTinThuoc(ID);
            if (table.Rows.Count == 0)
                return false;
            foreach (DataRow r in table.Rows)
            {
                this.ID                     = Int32.Parse(r["ID"].ToString());
                this.MaThuoc                = r["MaThuoc"].ToString();
                this.TenThuoc               = r["TenThuoc"].ToString();
                this.NhomThuocID            = Int32.Parse(r["ID1"].ToString());
                this.TenNhom                = r["TenNhom"].ToString();
                this.HangSXID               = Int32.Parse(r["ID2"].ToString());
                this.TenHangSX              = r["TenHangSX"].ToString();
                this.QuocGia                = r["QuocGia"].ToString();
                this.HinhThuc               = r["HinhThuc"].ToString();
                this.HamLuong               = r["HamLuong"].ToString();
                this.LieuLuongCachDung      = r["LieuLuongCachDung"].ToString();
                this.ChiDinh                = r["ChiDinh"].ToString();
                this.ChongChiDinh           = r["ChongChiDinh"].ToString();
                this.DuocDongHoc            = r["DuocDongHoc"].ToString();
                this.DuocLucHoc             = r["DuocLucHoc"].ToString();
                this.ThanTrong              = r["ThanTrong"].ToString();
                this.TacDungPhu             = r["TacDungPhu"].ToString();
                this.LuuY                   = r["LuuY"].ToString();
                this.GhiChu                 = r["GhiChu"].ToString();
            }
            return true;
        }

        public bool insertThuoc()
        {
            DBConnector dbc = new DBConnector();
            Dictionary<String, String> data = new Dictionary<String, String>();
            data.Add("ID", this.ID.ToString());
            data.Add("MaThuoc", "THU" + Session.generateID(5, this.ID));
            data.Add("TenThuoc",    this.TenThuoc);
            data.Add("NhomThuocID", this.NhomThuocID.ToString());
            data.Add("HangSXID", this.HangSXID.ToString());
            data.Add("HinhThuc", this.HinhThuc);
            data.Add("HamLuong", this.HamLuong);
            data.Add("LieuLuongCachDung", this.LieuLuongCachDung);
            data.Add("ChiDinh", this.ChiDinh);
            data.Add("ChongChiDinh", this.ChongChiDinh);
            data.Add("DuocDongHoc", this.DuocDongHoc);
            data.Add("DuocLucHoc", this.DuocLucHoc);
            data.Add("ThanTrong", this.ThanTrong);
            data.Add("TacDungPhu", this.TacDungPhu);
            data.Add("LuuY", this.LuuY);
            data.Add("GhiChu", this.GhiChu);
                                      
            try
            {
                dbc.Insert("Thuoc", data);
            }
            catch (Exception crap)
            {
                MessageBox.Show(crap.Message);
                return false;
            }
            MessageBox.Show("Thêm Thuốc mới thành công!");
            this.setMaThuoc();
            return true;

        }

        public bool updateThuoc()
        {
            DBConnector dbc = new DBConnector();
            Dictionary<String, String> data = new Dictionary<String, String>();
            data.Add("MaThuoc", this.MaThuoc);
            data.Add("TenThuoc", this.TenThuoc);
            data.Add("NhomThuocID", this.NhomThuocID.ToString());
            data.Add("HangSXID", this.HangSXID.ToString());
            data.Add("HinhThuc", this.HinhThuc);
            data.Add("HamLuong", this.HamLuong);
            data.Add("LieuLuongCachDung", this.LieuLuongCachDung);
            data.Add("ChiDinh", this.ChiDinh);
            data.Add("ChongChiDinh", this.ChongChiDinh);
            data.Add("DuocDongHoc", this.DuocDongHoc);
            data.Add("DuocLucHoc", this.DuocLucHoc);
            data.Add("ThanTrong", this.ThanTrong);
            data.Add("TacDungPhu", this.TacDungPhu);
            data.Add("LuuY", this.LuuY);
            data.Add("GhiChu", this.GhiChu);
            try
            {
                dbc.Update("Thuoc", data, String.Format("ID = {0}", this.ID));
            }
            catch (Exception crap)
            {
                MessageBox.Show(crap.Message);
                return false;
            }
            MessageBox.Show("Cập nhật Thuốc thành công!");
            return true;
        }

        public bool deleteThuoc()
        {
            if (MessageBox.Show("Bạn thật sự muốn xóa Thuốc này ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
            {
                DBConnector dbc = new DBConnector();
                return dbc.Delete("Thuoc", String.Format("ID = {0}", this.ID));
            }
            else
                return false;
        }

        public int getMaxID()
        {
            DBConnector dbc = new DBConnector();
            String sql = "SELECT MAX(ID) as MAXID FROM Thuoc";
            return this.ID = Int32.Parse(dbc.GetDataTable(sql).Rows[0]["MAXID"].ToString()) + 1;
        }

        public void setMaThuoc()
        {
            DBConnector dbc = new DBConnector();
            String sql = "SELECT MaThuoc FROM Thuoc WHERE Thuoc.ID = " + this.ID;
            this.MaThuoc = dbc.GetDataTable(sql).Rows[0]["MaThuoc"].ToString();
        }

    }        
}
